﻿using System.Net;
using System.Net.Sockets;
using System.Text;
using Microsoft.SPOT;

public class EfficientPutRequest
{
    public static void Main()
    {
        const string apiKey = "<insert your API key here>";
        const string feedId = "<insert your feed ID here>";

        // this is the "sample" we want to send to Pachube
        var sample = "number,43";
        // convert sample to byte array
        byte[] contentBuffer = Encoding.UTF8.GetBytes(sample);

        // produce request
        using (Socket connection = Connect("api.pachube.com", 5000))
        {
            SendRequest(connection, apiKey, feedId, sample);
        }
    }

    static Socket Connect(string host, int timeout)
    {
        // look up host's domain name to find IP address(es)
        IPHostEntry hostEntry = Dns.GetHostEntry(host);
        // extract a returned address
        IPAddress hostAddress = hostEntry.AddressList[0];
        IPEndPoint remoteEndPoint = new IPEndPoint(hostAddress, 80);

        // connect!
        Debug.Print("connect...");
        var connection = new Socket(AddressFamily.InterNetwork,
            SocketType.Stream, ProtocolType.Tcp);
        connection.Connect(remoteEndPoint);
        connection.SetSocketOption(SocketOptionLevel.Tcp,
            SocketOptionName.NoDelay, true);
        connection.SendTimeout = timeout;
        return connection;
    }

    static void SendRequest(Socket s, string apiKey, string feedId,
        string content)
    {
        byte[] contentBuffer = Encoding.UTF8.GetBytes(content);
        const string CRLF = "\r\n";
        var requestLine =
            "PUT /v2/feeds/" + feedId + ".csv HTTP/1.1" + CRLF;
        byte[] requestLineBuffer = Encoding.UTF8.GetBytes(requestLine);
        var headers =
            "Host: api.pachube.com" + CRLF +
            "X-PachubeApiKey: " + apiKey + CRLF +
            "Content-Type: text/csv" + CRLF +
            "Content-Length: " + contentBuffer.Length + CRLF +
            CRLF;
        byte[] headersBuffer = Encoding.UTF8.GetBytes(headers);
        s.Send(requestLineBuffer);
        s.Send(headersBuffer);
        s.Send(contentBuffer);
    }
}
